﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace AltimilliaLibrary.Base_Object
{
  public class BaseManager : IUpdateable, IDrawable, IGameComponent
  {
    private List<IAltimilliaGameComponent> list;
    private bool IsIntitalized;

    public BaseManager()
    {
      list = new List<IAltimilliaGameComponent>();
      IsIntitalized = false;
    }

    public void Initialize()
    {
      foreach (IAltimilliaGameComponent gc in list)
      {
        gc.Initialize();
      }
      IsIntitalized = true;
    }
    public void Update(GameTime gameTime)
    {
      List<IAltimilliaGameComponent> toUpdate = new List<IAltimilliaGameComponent>();
      foreach (IAltimilliaGameComponent gc in list)
      {
        toUpdate.Add(gc);
      }
      foreach (IAltimilliaGameComponent gc in toUpdate)
      {
        UpdateItem(gameTime,gc);
      }

    }

    public virtual void AddObject(IAltimilliaGameComponent Obj)
    {
      Obj.Manager = this;
      if (IsIntitalized)
      {
        Obj.Initialize();
      }
      this.list.Add(Obj);
    }
    public virtual bool RemoveObject(IAltimilliaGameComponent Obj)
    {
      return this.list.Remove(Obj);
    }

    protected virtual void UpdateItem(GameTime gameTime, IAltimilliaGameComponent gc)
    {
      gc.Update(gameTime);
    }
    public void Draw(GameTime gameTime)
    {
      foreach (IAltimilliaGameComponent gc in list)
      {
        DrawItem(gameTime, gc);
      }
    }
    public virtual void DrawItem(GameTime gameTime, IAltimilliaGameComponent gc)
    {
      gc.Draw(gameTime);
    }


    public int DrawOrder { get; set; }
    public event EventHandler DrawOrderChanged;
    public bool Visible { get; set; }
    public event EventHandler VisibleChanged;
    public bool Enabled { get; set; }
    public event EventHandler EnabledChanged;
    public int UpdateOrder { get; set; }
    public event EventHandler UpdateOrderChanged;
  }
}
